﻿using System;
using System.Windows;
using Kiva7.DataClasses;
using Kiva7.Utilities;
using Image = System.Windows.Controls.Image;

namespace Kiva7.Controls
{
    public partial class KivaImageControl 
    {
        public KivaImageControl()
        {
            InitializeComponent();

            image.ImageOpened += ImageOpened;
        }

        protected override Image Image
        {
            get
            {
                return image;
            }
        }

        public ImageSizes ImageSize { get; set; }

        public Kiva7.DataClasses.Image ImageData
        {
            get { return (Kiva7.DataClasses.Image)GetValue(ImageProperty); }
            set { SetValue(ImageProperty, value); }
        }

        public static readonly DependencyProperty ImageProperty =
                        DependencyPropertyExt.Register<KivaImageControl, Kiva7.DataClasses.Image>(x => x.ImageData,
                                                                             new PropertyMetadata(EntityChanged));

        private static void EntityChanged(DependencyObject d,
                                     DependencyPropertyChangedEventArgs e)
        {
            KivaImageControl pc = d as KivaImageControl;
            if (pc != null)
            {
                pc.EntityChanged();
            }

        }

        public override Uri CreateImageUri()
        {
            if (ImageData != null)
            {
                return Kiva7.DataClasses.ImageSize.CreateImageUri(ImageSize, ImageData.Id);
            }
            return null;
        }

        public override string GetImageDirectoryName()
        {
            return Kiva7.DataClasses.ImageSize.GetImageString(ImageSize);
        }

        public override string GetImagePath()
        {
            if (this.ImageData == null)
            {
                return null;
            }
            return System.IO.Path.Combine(GetImageDirectoryName(), this.ImageData.Id + ".JPG");
        }

    }
}
